#include "YouwantDeclar.h"

#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QJsonValue>

YouwantDeclar::YouwantDeclar(): d_ptr(new YouwantDeclarPrivate(this))
{
    
}

void YouwantDeclar::setLabel(QString label) {
    Q_D(YouwantDeclar);
    d->label = label;
}
QString YouwantDeclar::getLabel() {
    Q_D(YouwantDeclar);
    return d->label;
}
void YouwantDeclar::setDetail(QString detail) {
    Q_D(YouwantDeclar);
    d->detail = detail;
}
QString YouwantDeclar::getDetail() {
    Q_D(YouwantDeclar);
    return d->detail;
}
void YouwantDeclar::setType(QString type) {
    Q_D(YouwantDeclar);
    d->type = type;
}
QString YouwantDeclar::getType() {
    Q_D(YouwantDeclar);
    return d->type;
}
void YouwantDeclar::setShellCommands(QStringList shellcommands) {
    Q_D(YouwantDeclar);
    d->shellcommands = shellcommands;
}
QStringList YouwantDeclar::getShellCommands() {
    Q_D(YouwantDeclar);
    return d->shellcommands;
}

QString YouwantDeclar::toJson() {
    Q_D(YouwantDeclar);

    QJsonObject object;
    QJsonObject temp;
    QJsonObject shell;
    QJsonArray commands;

// 从 commands 最深开始
    commands = 
        QJsonArray::fromStringList(d->shellcommands);
// commands 到达 shell
    shell.insert("commands", commands);
// shell 到达 template
    temp.insert("shell", shell);
    temp.insert("action", "shell");

// tempatle 到达最外层
    object.insert("label", d->label);
    object.insert("detail", d->detail);
    object.insert("type", d->type);
    object.insert("template", temp);

    return QJsonDocument(object).toJson();
}


// {
//     "detail": "获取一个 java-sources-set 模板",
//     "label": "获取一个 java-sources-set 模板",
//     "type": "java|source|set"
//     "template": {
//         "action": "shell",
//         "shell": {
//             "commands": [
//                 "mkdir -p src/main/java",
//                 "mkdir -p src/main/resources",
//                 "mkdir -p src/test/java",
//                 "mkdir -p src/test/resources"
//             ]
//         }
//     },
// }